/**
 * @(#)SearchDataServiceImpl.java 2017年6月2日
 * Copyright(C) 2017年6月2日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IChannelApkConfigDAO;
import org.jsbd.boss.dao.IDmChannelRetentDAO;
import org.jsbd.boss.dao.IImsiRetentDao;
import org.jsbd.boss.dao.IMiguDataStatisticsDAO;
import org.jsbd.boss.dao.INewStatisticsDao;
import org.jsbd.boss.dao.IRetentStatisticsDao;
import org.jsbd.boss.dao.impl.ISimulatorActionLogDAO;
import org.jsbd.boss.domian.ChannelRetentLogInfo;
import org.jsbd.boss.domian.ImsiRetentSh;
import org.jsbd.boss.domian.MiguDataSh;
import org.jsbd.boss.domian.TaskHandlerSh;
import org.jsbd.boss.service.ISearchDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO 请加入本类的说明
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
@Service("searchDataService")
public class SearchDataServiceImpl implements ISearchDataService {
	
	public static final Logger LOGGER = LoggerFactory.getLogger(SearchDataServiceImpl.class);

	@Autowired
	private ISimulatorActionLogDAO simulatorActionLogDao;
	@Autowired
	private IImsiRetentDao imsiRetentDao;
	@Autowired
	private IRetentStatisticsDao retentStatisticsDao;
	@Autowired
	private INewStatisticsDao newStatisticsDao;
	@Autowired
	private IChannelApkConfigDAO channelApkConfigDAO;
	@Autowired
	private IMiguDataStatisticsDAO miguDataStatisticsDao;
	@Autowired
	private IDmChannelRetentDAO dmchannelRetentDao;
	
	@Override
	public List<MiguDataSh> getMiguDataShList(MiguDataSh miguDataSh) {
		List<MiguDataSh> miguDataShList=simulatorActionLogDao.getMiguDataList(miguDataSh);
		MiguDataSh total=new MiguDataSh();
		total.setMark("总共");
		total.setSuccessNum(0);
		total.setSearchDate(miguDataSh.getSearchDate());
		
		if(miguDataShList==null||miguDataShList.isEmpty()){
			miguDataShList=new ArrayList<MiguDataSh>();
			miguDataShList.add(total);
			return miguDataShList;
		}
		
		int count=0;
		for(MiguDataSh temp:miguDataShList){
			count+=temp.getSuccessNum();
		}
		total.setSuccessNum(count);
		miguDataShList.add(0, total);
		
		
		
		return miguDataShList;
	}

	@Override
	public Map<String, Object> getImsiRetentPageList(DataGridModel page, ImsiRetentSh imsiRetentSh) {
		return imsiRetentDao.getImsiRetentPageList(page, imsiRetentSh);
	}

	@Override
	public Map<String, Object> getImsiRetentLogPageList(DataGridModel page, ImsiRetentSh imsiRetentSh) {
		return imsiRetentDao.getImsiRetentLogPageList(page, imsiRetentSh);
	}

	
	@Override
	public Map<String, Object> getNewTaskPageList(DataGridModel page, TaskHandlerSh taskHandlerSh) {
		Map<String, Object> resultMap=newStatisticsDao.getNewTaskPageList(page, taskHandlerSh);
//		List<TaskHandlerSh> dataList=(List<TaskHandlerSh>)resultMap.get("rows");
//		if(dataList==null||dataList.isEmpty()){
//			return resultMap;
//		}
//		
//		List<ChannelApkConfig>  configList=channelApkConfigDAO.queryApkInfos(null);
//		if(configList==null||configList.isEmpty()){
//			return resultMap;
//		}
//		Map<String,ChannelApkConfig> apkMap=new HashMap<String,ChannelApkConfig>();
//		Iterator<ChannelApkConfig> itConfig=configList.iterator();
//		while(itConfig.hasNext()){
//			ChannelApkConfig config=itConfig.next();
//			apkMap.put(config.getMark(), config);
//		}
//		
//		Iterator<TaskHandlerSh> taskShIt=dataList.iterator();
//		while(taskShIt.hasNext()){
//			TaskHandlerSh taskSh=taskShIt.next();
//			ChannelApkConfig config=apkMap.get(taskSh.getMark());
//			if(config!=null){
//				taskSh.setAppName(config.getApkName());
//				if(taskSh.getTotalNum()==null||taskSh.getTotalNum()==0){
//					taskSh.setTotalNum(config.getDaylimit());
//				}
//			}
//			
//			if(StringUtils.isNotBlank(taskHandlerSh.getAppName())){
//				if(StringUtils.isBlank(taskSh.getAppName())){
//					taskShIt.remove();
//					continue;
//				}
//				
//				if(!taskSh.getAppName().contains(taskHandlerSh.getAppName())){
//					taskShIt.remove();
//					continue;
//				}
//			}
//		}
		return resultMap;
	}

	@Override
	public Map<String, Object> getRetentTaskPageList(DataGridModel page, TaskHandlerSh taskHandlerSh) {
		Map<String, Object> resultMap=retentStatisticsDao.getRetentTaskPageList(page, taskHandlerSh);
//		List<TaskHandlerSh> dataList=(List<TaskHandlerSh>)resultMap.get("rows");
//		if(dataList==null||dataList.isEmpty()){
//			return resultMap;
//		}
//		
//		List<ChannelApkConfig>  configList=channelApkConfigDAO.queryApkInfos(null);
//		if(configList==null||configList.isEmpty()){
//			return resultMap;
//		}
//		
//		Map<String,String> apkMap=new HashMap<String,String>();
//		Iterator<ChannelApkConfig> itConfig=configList.iterator();
//		while(itConfig.hasNext()){
//			ChannelApkConfig config=itConfig.next();
//			apkMap.put(config.getMark(), config.getApkName());
//		}
//		
//		Iterator<TaskHandlerSh> taskShIt=dataList.iterator();
//		while(taskShIt.hasNext()){
//			TaskHandlerSh taskSh=taskShIt.next();
//			taskSh.setAppName(apkMap.get(taskSh.getMark()));
//			
//			if(StringUtils.isNotBlank(taskHandlerSh.getAppName())){
//				if(StringUtils.isBlank(taskSh.getAppName())){
//					taskShIt.remove();
//					continue;
//				}
//				
//				if(!taskSh.getAppName().contains(taskHandlerSh.getAppName())){
//					taskShIt.remove();
//					continue;
//				}
//			}
//		}
		
		return resultMap;
	}

	@Override
	public List<MiguDataSh> getMiguDataStatisticsList(MiguDataSh miguDataSh) {
		List<MiguDataSh> miguDataShList=miguDataStatisticsDao.getMiguDataStatisticsList(miguDataSh);
		MiguDataSh total=new MiguDataSh();
		total.setMark("总共");
		total.setSuccessNum(0);
		total.setSearchDate(miguDataSh.getSearchDate());
		
		if(miguDataShList==null||miguDataShList.isEmpty()){
			miguDataShList=new ArrayList<MiguDataSh>();
			miguDataShList.add(total);
			return miguDataShList;
		}
		
		int count=0;
		List<String> marks = new ArrayList<String>();
		
		for(MiguDataSh temp:miguDataShList){
			count+=temp.getSuccessNum();
			marks.add(temp.getMark());
		}
		List<Map<String, String>> list = channelApkConfigDAO.findApknameByMarks(marks);
		Map<String, String> map = new HashMap<String, String>();
		for (Map<String, String> temp : list) {
			map.put(temp.get("mark"), temp.get("apkName"));
		}
		for (MiguDataSh temp:miguDataShList) {
			String mark = map.get(temp.getMark());
			temp.setApkName(mark == null ? "" : mark);
		}
		
		total.setSuccessNum(count);
		miguDataShList.add(0, total);
		
		return miguDataShList;
	
	}

	@Override
	public Map<String, Object> getNewTaskPageMonthList(DataGridModel page, TaskHandlerSh taskHandlerSh) {
		Map<String, Object> resultMap=newStatisticsDao.getNewTaskPageMonthList(page, taskHandlerSh);
		return resultMap;
	}

	@Override
	public List<ChannelRetentLogInfo> getCalibrationByDate(String calibrationDay) {
		ChannelRetentLogInfo channelRetentLogInfo = new ChannelRetentLogInfo();
		channelRetentLogInfo.setCurrentDate(calibrationDay);
		return dmchannelRetentDao.selectChannelRetentLogOfDay(channelRetentLogInfo);
	}

}
